package com.game77.guessTheWords2.layer;

import android.graphics.Typeface;
import android.widget.Toast;

import com.game77.guessTheWords2.MainActivity;
import com.game77.guessTheWords2.R;
import com.game77.guessTheWords2.core.GameSystem;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.scene.MainScene;
import com.game77.guessTheWords2.scene.ToolShopScene;
import com.game77.guessTheWords2.util.AudioHelper;
import com.game77.guessTheWords2.util.GameUtil;
import com.game77.guessTheWords2.util.offer.OfferHelper;
import com.umeng.analytics.MobclickAgent;
import com.wiyun.engine.nodes.AtlasLabel;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Layer;
import com.wiyun.engine.nodes.Sprite;
import com.wiyun.engine.types.WYColor3B;
import com.wiyun.engine.types.WYPoint;

public class ToolShopLayer extends Layer {

	Sprite background;
	Button btnBuyLamp;
	Button btnGetCoins;
	Label currentLampCount;
	AtlasLabel currentGoldCount;
	Button btnReturn;

	public ToolShopLayer() {
		// 背景
		background = Sprite.make(GameSystem.tex_shop_background);
		background.setAnchor(0, 0);
		background.setPosition(WYPoint.make(0, 0));
		background.setScaleX(GameSystem.Screen_Width
				/ GameSystem.tex_game_background.getWidth());
		background.setScaleY(GameSystem.Screen_Height
				/ GameSystem.tex_game_background.getHeight());
		this.addChild(background);

		// return button
		btnReturn = Button.make(R.drawable.btn_gameback,
				R.drawable.btn_gameback_press, this, "onReturnClick");
		btnReturn.setPosition(438, 755);
		addChild(btnReturn);

		// 当前金币
		currentGoldCount = AtlasLabel.make(StageManager.instance.getGoldCount()
				+ "", GameSystem.tex_numbers_48, GameSystem.charMap);
		currentGoldCount.setColor(WYColor3B.make(255, 255, 0));
		currentGoldCount.setAnchorX(0);
		currentGoldCount.setPosition(130, 640);
		addChild(currentGoldCount);

		// 购买道具
		btnBuyLamp = Button.make(R.drawable.button_buy_normal,
				R.drawable.button_buy_pressed, this, "onBuyLampClick");
		btnBuyLamp.setPosition(335, 365);
		addChild(btnBuyLamp);

		// 获取金币
		btnGetCoins = Button.make(R.drawable.button_getgold_normal,
				R.drawable.button_getgold_pressed, this, "onGetGoldClick");
		btnGetCoins.setPosition(240, 95);
		addChild(btnGetCoins);

		currentLampCount = Label.make(
				StageManager.instance.getLampCount() + "", 35, Typeface.BOLD);
		currentLampCount.setAnchorX(0);
		currentLampCount.setColor(WYColor3B.make(255, 255, 255));
		currentLampCount.setPosition(185, 365);
		addChild(currentLampCount);
	}

	public void updateCount() {
		currentGoldCount.setText(StageManager.instance.getGoldCount() + "");
		currentLampCount.setText(StageManager.instance.getLampCount() + "");

	}

	public void onGetGoldClick() {
		AudioHelper.clickButton();
		// 打开墙
		OfferHelper.showOffer(MainActivity.instance, null, null);
	}

	public synchronized void onBuyLampClick() {
		// 购买一个提示
		if (StageManager.instance.getGoldCount() >= 5) {
			StageManager.instance.setGoldCount(StageManager.instance
					.getGoldCount() - 5);
			StageManager.instance.setLampCount(StageManager.instance
					.getLampCount() + 1);

			MainActivity.instance.runOnUiThread(new Runnable() {
				@Override
				public void run() {
					MobclickAgent.onEvent(MainActivity.instance, "buyLamp");
				}
			});

			AudioHelper.buy();
			// 更新界面
			updateCount();
		} else {
			// 提示金币不足
			OfferHelper.showOffer(MainActivity.instance, null, "金币不足是否免费赚取金币");
		}
	}

	public void onReturnClick() {
		AudioHelper.clickButton();
		ToolShopScene.make().returnLastScene();
	}
}
